﻿using Common.Result;
using Common.Utils;
using Interface;
using IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.Dto.Users;
using Model.Entitys;
using NetTaste;
using StackExchange.Redis;

namespace TakeawayWebApi.Controllers
{
    //[Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        //private readonly ;
        private readonly ILogger<UserController> _logger;
        private readonly IUserService _userService;
        private readonly ICustomJWTService _jWTService;
        public UserController(ILogger<UserController> logger,IUserService userService, ICustomJWTService jWTService)
        {
            _logger = logger;
            _userService = userService;
            _jWTService = jWTService;
        }
        
        /// <summary>
        /// 发送手机短信验证码
        /// </summary>
        [HttpPost("sendMsg")]
        public async Task<ApiResult> SendMsg(UserLogin userLogin)
        {
            if (await _userService.SendMsgAsync(userLogin))
            {
                return ResultHelper.Success("手机验证码短信发送成功");
            }
            return ResultHelper.Error("短信发送失败");
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userLogin"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public async Task<ApiResult> Login(UserLogin userLogin)
        {
            var user = await _userService.LoginAsync(userLogin);
            if (user!=null)
            {
                return ResultHelper.Success(await _jWTService.GetTokenAsync(user));
            }
            return ResultHelper.Error("登录失败");
        }
    }
}
